mod common;
use crate::code::ck_code;
use crate::common::*;
use serde_json::json;

#[tokio::test]
async fn test_code_1() {
    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "code",
        "list",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "code1",
                "name": "name_code1",
            }
        }),
        "code",
        "add",
    )
    .await;

    ck_code(
        pos!(),
        json!({
            "all": ["code1"],
            "code1": {"name": "name_code1", "sn": "code1"},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "code",
        "list",
        json!([{"name": "name_code1", "sn": "code1"}]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "code1",
        }),
        "code",
        "get",
        json!({"name": "name_code1", "sn": "code1"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "code1",
        }),
        "code",
        "rm",
    )
    .await;

    ck_code(pos!(), json!({})).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "code",
        "list",
        e_node(),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "code1",
        }),
        "code",
        "get",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "code1",
        }),
        "code",
        "rm",
    )
    .await;

    ck_code(pos!(), json!({})).await;
}

#[tokio::test]
async fn test_code_2() {
    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "code1",
                "name": "name_code1",
            }
        }),
        "code",
        "add",
    )
    .await;

    ck_code(
        pos!(),
        json!({
            "all": ["code1"],
            "code1": {"name": "name_code1", "sn": "code1"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "code2",
                "name": "name_code2",
            }
        }),
        "code",
        "add",
    )
    .await;

    ck_code(
        pos!(),
        json!({
            "all": ["code1", "code2"],
            "code1": {"name": "name_code1", "sn": "code1"},
            "code2": {"name": "name_code2", "sn": "code2"},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "code",
        "list",
        json!([
                {"sn": "code1", "name": "name_code1"},
                {"sn": "code2", "name": "name_code2"},
        ]),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": [{
                "sn": "code1",
                "name": "n_code1",
            }]
        }),
        "code",
        "md",
    )
    .await;

    ck_code(
        pos!(),
        json!({
            "all": ["code1", "code2"],
            "code1": {"name": "n_code1", "sn": "code1"},
            "code2": {"name": "name_code2", "sn": "code2"},
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "code",
        "list",
        json!([
                {"sn": "code1", "name": "n_code1"},
                {"sn": "code2", "name": "name_code2"},
        ]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "code1",
        }),
        "code",
        "get",
        json!({"sn": "code1", "name": "n_code1"}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "code1",
        }),
        "code",
        "rm",
    )
    .await;

    ck_code(
        pos!(),
        json!({
            "all": ["code2"],
            "code2": {"name": "name_code2", "sn": "code2"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "code2",
        }),
        "code",
        "rm",
    )
    .await;

    ck_code(pos!(), json!({})).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "code",
        "list",
        e_node(),
    )
    .await;
}
